const { renameSync } = require('fs')
const pathURL = require('../utils/path')
const isDirExisted = require('../utils/exist')
const { validateFileName } = require('../utils/validate')
// 重写目录名称
const renameDir = (comArr) => {
    const [, oldName, newName] = comArr
    if (isDirExisted(oldName)) {
        // 目录名是否包含非法字符
        if (validateFileName(newName)) {
            process.send('修改错误， 目录名不能包含以下任何字符： / \ : * ? " < > | ')
            return
        }
        try {
            renameSync(pathURL(oldName), pathURL(newName))
            process.send(`修改${oldName} -> ${newName} 目录成功`)
        } catch (error) {
            process.send('修改错误')
        }
    } else {
        process.send('没有该目录')
    }
}

const modifyDir = (command = 'modifyDir a b') => {
    const comArr = command.split(' ');
    if (comArr.length === 3) {
        renameDir(comArr)
    } else {
        process.send('修改目录格式有误; 请查看帮助 -h')
    }
}


process.on('message', modifyDir)